<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		   $this->load->model('Mproduct_cat');
		   $this->load->library('pagination');
		   $this->lang->load('home', 'vietnamese');
	}
	
	function index($page='')
	{
		$limit = 9;

		$config['base_url'] = site_url().'/menu/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 6;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mproduct_cat->count_all('tbl_product_cat');

		$config['per_page'] = $limit;
		
		$this->pagination->initialize($config);
	
    if (empty($page))	$page = 0; //default value
		$data['title']='Sản phẩm';
		$data['main']= 'menupage';
		$lg =1;
		$data['result'] = $this->Mproduct_cat->get_catdata($lg);
		$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$this->load->view('template/index_info',$data);
	}
	function detail($id)
	{
			$lg = 'vn';
			if($lg == 'en')
				$lang = 2;
			else
				$lang =1;
			$data['title']='Sản phẩm';
			 $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		
			$data['main']='product_detail';
		$data['result']= $this->Mproduct->get_prod_detail($id,$lang);
		$this->load->view('template/index',$data);
	
	}
	function category($id,$page='')
	{
		$lg = 'vn';
			if($lg == 'en')
				$lang = 2;
			else
				$lang =1;
		$limit = 9;

		$config['base_url'] = site_url().'/products/category/'.$id.'/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mproduct->count_prod_from_cate($id);
		
		
		$config['per_page'] = $limit;
		
		$this->pagination->initialize($config);
		
    if (empty($page))	$page = 0; //default value
		 $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Sản phẩm';
		$data['main']='productpage';
		$data['result']= $this->Mproduct->get_prod_cat($id,$lang,$page,$limit);
		$this->load->view('template/index',$data);
	}
	
	/*** cart ***/
	
	function send_order()
	{
		$to = "tuongdang@nhunguyenco.com";
$subject = "SimpleModal Contact Form";

// Include extra form fields and/or submitter data?
// false = do not include
$extra = array(
	"form_subject"	=> false,
	"form_cc"		=> true,
	"ip"			=> true,
	"user_agent"	=> true
);

// Process
$action = isset($_POST["action"]) ? $_POST["action"] : "";
if (empty($action)) 
{
	// Send back the contact form HTML
	$output = "<div style='display:none'>
	<div class='contact-top'></div>
	<div class='contact-content'>
		<h1 class='contact-title'>Thông tin đặt hàng:</h1>
		<div class='contact-loading' style='display:none'></div>
		<div class='contact-message' style='display:none'></div>
		<form action='#' style='display:none'>
			<label for='contact-name'>*Họ tên:</label>
			<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
			<label for='contact-email'>*Email:</label>
			<input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />
			<label for='contact-phone'>*Điện thoại:</label>
			<input type='text' id='contact-phone' class='contact-input' name='phone' tabindex='1002' />
			
			";

	$output .= "
			<label for='contact-message'>*Nội dung :</label>
			<textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea>
			<br/>";

	$output .= "
			<label>&nbsp;</label>
			<button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
			<button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
			<br/>
			<input type='hidden' name='token' value='" . smcf_token($to) . "'/>
		</form>
	</div>
	<div class='contact-bottom'><a href='http://www.vanphongpham.com/'>Văn phòng phẩm</a></div>
</div>";

	echo $output;
}
else if ($action == "send") {
	// Send the email
	$name = isset($_POST["name"]) ? $_POST["name"] : "";
	$email = isset($_POST["email"]) ? $_POST["email"] : "";
	$subject = isset($_POST["subject"]) ? $_POST["subject"] : $subject;
	$message = isset($_POST["message"]) ? $_POST["message"] : "";
	$cc = isset($_POST["cc"]) ? $_POST["cc"] : "";
	$token = isset($_POST["token"]) ? $_POST["token"] : "";
	
	// make sure the token matches
	
		//smcf_send($name, $email, $subject, $message, $cc);
		$now = time();
//$now = '';
				$data =array (
					'order_name' => $this->input->post("name"),
					'order_phone' => $this->input->post("phone"),
					'order_email' => $this->input->post("email"),
					'order_detail' => $this->input->post("message"),
					'order_modify' => $now,
					'order_status' => 0,
				);
				$this->load->model('Mproduct');
				 
		if(	$this->Mproduct->insert_order($data)) 
		{
		
		echo "Your message was successfully sent.";
	}
	else {
		echo "Unfortunately, your message could not be verified.";
	}
}

}

function smcf_token($s) {
	return md5("smcf-" . $s . date("WY"));
}

function sendmail()
{
	//$title = "'Anh mười' - Thư Liên hệ ";
//	$body = "Họ Tên: ".$this->input->post("name")."<br /> Từ Email:".$this->input->post("email")."<br />Tiêu Đề: ".$this->input->post("title")."<br />Nội dung: ".$this->input->post("detail");
			
			//	$email="tuong.dangman@gmail.com";
				$now = time();
				$data =array (
					'order_name' => $this->input->post("name"),
					'order_phone' => $this->input->post("phone"),
					'order_email' => $this->input->post("email"),
					'order_subject' => $this->input->post("title"),
					'order_detail' => $this->input->post("message"),
					'order_modify' => $now,
					'order_status' => '0',
				);
			//	$this->load->model('Mproduct');
				 
				if(	$this->Mproduct->insert_order($data)  )
					return true;
				return false;
				
}


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */